<template>
  <div >
    <table
      border="1"
      width="700"
      style="border-collapse: collapse"
    >
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model="isAll" /> <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr class="my_div" v-for="(item,index) in sj" :key="index">
          <th>
            <input type="checkbox"  v-model="item.checked"/>
          </th>
          <th>{{item.name}}</th>
          <th>{{item.price}}</th>
          <th><button @click="item.num>0?item.num--:0">-</button>{{item.num}}<button @click="item.num++">+</button></th>
          <th>{{item.price*item.num}}</th>
          <th><button @click="item.num=1,item.checked=false">删除</button></th>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">
            {{total}}
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  props: ['sj'],
  data () {
    return {
    }
  },
  methods: {

  },
  computed: {
    isAll: {
      get () {
        return this.sj.every(item => item.checked)
      },
      set (value) {
        this.sj.forEach(item => {
          item.checked = value
        })
      }
    },
    total () {
      const totals = this.sj.filter(item => {
        return item.checked === true
      })
      return totals.reduce((sum, obj) => {
        sum += obj.price * obj.num
        return sum
      }, null)
    }
  }

}
</script>

<style>
</style>
